﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SalaryChannel
{
    public partial class PhotoComment : UserControl
    {
        
        /// <summary>
        /// Event of MouseLeftButtonDown
        /// </summary>
        public new event MouseButtonEventHandler MouseLeftButtonDown;

        private string PhotoID = string.Empty;

        public new Visibility Visibility
        {
            get
            {
                return base.Visibility;
            }
            set
            {
                base.Visibility = value;
                if (value == System.Windows.Visibility.Visible)
                {
                    BindData(PhotoID);
                }
            }
        }

        public void BindData(string PhotoID)
        {
            if (string.IsNullOrEmpty(PhotoID))
            {
                return;
            }
            this.PhotoID = PhotoID;

            try
            {
                ServiceInstance.ViewService.GetCommentsAsync(PhotoID);
                IsReady = false;
            }
            catch 
            {

                App.MessageBox.ShowDialog("无法获取评论", Title.Error);
            }
        }

        public PhotoComment()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceInstance.ViewService.GetCommentsCompleted += new EventHandler<SalaryChannel.ViewService.GetCommentsCompletedEventArgs>(ViewService_GetCommentsCompleted);
            ServiceInstance.ViewService.InsertCommentCompleted += new EventHandler<SalaryChannel.ViewService.InsertCommentCompletedEventArgs>(ViewService_InsertCommentCompleted);
        }

        void ViewService_InsertCommentCompleted(object sender, SalaryChannel.ViewService.InsertCommentCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    newComment.Text = string.Empty;

                    ServiceInstance.ViewService.GetCommentsAsync(PhotoID);
                }
                else
                {
                    App.MessageBox.ShowDialog(Properties.Resource.TryAgain, Title.Error);
                    IsReady = true;
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog(Properties.Resource.TryAgain, Title.Error);
                IsReady = true;
            }
        }

        void ViewService_GetCommentsCompleted(object sender, SalaryChannel.ViewService.GetCommentsCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("获取评论失败", Title.Error);
                }
                else
                {
                    commentList.ItemsSource = e.Result;
                }
            }
            catch
            {
                App.MessageBox.ShowDialog("获取评论失败", Title.Error);
            }
            finally
            {
                IsReady = true;
            }
        }

        private void PhotoCommentButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MouseLeftButtonDown != null)
            {
                MouseLeftButtonDown(sender, e);
            }

        }

        //private void EditImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{

        //}

        //private void toolImage_MouseEnter(object sender, MouseEventArgs e)
        //{

        //}

        //private void toolImage_MouseLeave(object sender, MouseEventArgs e)
        //{

        //}

        //private void DeleteImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{

        //}

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            this.newComment.Text = string.Empty;
            if (newCommentWarning.Visibility == Visibility.Visible)
            {
                newCommentWarning.Visibility = Visibility.Collapsed;

            }
        }

        private bool _IsReady = false;

        public bool IsReady
        {
            get { return _IsReady; }
            set
            { 
                _IsReady = value;
                this.Cursor = value ? Cursors.Arrow : Cursors.Wait;
            }
        }

        
        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            if (!IsReady) return;
            if (string.IsNullOrEmpty(PhotoID)) return;

            if (string.IsNullOrEmpty(newComment.Text.Trim()))
            {
                this.newCommentWarning.Visibility = Visibility.Visible;
                return;
            }
            try
            {
                ServiceInstance.ViewService.InsertCommentAsync(PhotoID, App.CurrentUser.UserID,
                        newComment.Text.Trim());
                IsReady = false;
            }
            catch 
            {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }
        }

        public void Clear()
        {
            this.commentList.ItemsSource = null;
            this.PhotoID = string.Empty;
            this.newComment.Text = "请输入您的评论";
            if (newCommentWarning.Visibility == Visibility.Visible)
            {
                newCommentWarning.Visibility = Visibility.Collapsed;

            }
        }

        private void newComment_GotFocus(object sender, RoutedEventArgs e)
        {
            if (newComment.Text == "请输入您的评论")
            {
                newComment.Text = string.Empty;
            }
        }
    }
}
